import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';

@Injectable()
export class UserService {
  @InjectRepository(User)
  private userRepository: Repository<User>;

  // 增加用户
  create(createUserDto: CreateUserDto) {
    return this.userRepository.save(createUserDto);
  }

  // 查询所有用户
  findAll() {
    return this.userRepository.find();
  }

  // 根据ID查询单个用户
  findOne(id: number) {
    return this.userRepository.findOneBy({
      id,
    });
  }

  // 根据ID修改用户
  update(id: number, updateUserDto: UpdateUserDto) {
    return this.userRepository.update(id, updateUserDto);
  }

  // 根据ID删除单个用户
  remove(id: number) {
    return this.userRepository.delete(id);
  }
}
